package yxy.game.pm2.active.command.holiday;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.atcholidayturntableactive;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_节日活动;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * Author HYB
 * 领取转盘次数奖励
 */
@MessageType(proto = Command.ActHolidayLotteryNumRewardRequest, classtype = ActiveMessage.ActHolidayLotteryNumRewardRequest.class)
public class CMD_Active_节日_领取转盘次数奖励 extends Command<ActiveMessage.ActHolidayLotteryNumRewardRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActHolidayLotteryNumRewardRequest request) throws Exception {
        String pcode = messageSource.getTag();
        int id = request.getId();
        ActiveMessage.ActHolidayLotteryNumRewardResponse.Builder response = ActiveMessage.ActHolidayLotteryNumRewardResponse.newBuilder();
        ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }

        DrawStatus cangainreward = am.canGainTurnTableActiveReward(id);
        if (cangainreward == DrawStatus.hadget) {
            send(messageSource, response, Result.ActHadGet);
            return;
        }
        if (cangainreward == DrawStatus.cannotget) {
            send(messageSource, response, Result.ActCannotGet);
            return;
        }
        am.setLotteryActiveDraw(id);
        atcholidayturntableactive data = DataCenter.getData(id, atcholidayturntableactive.class);
        if (data == null) {
            send(messageSource, response, Result.DataError);
            return;
        }
        Collection<Currency> cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());

        response.addAllDisplay(new CurrencyHandle().packItems(cur));

        //下发道具
        new CurrencyHandle().addCurrency(pcode, CCReason.活动_节日, cur);
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActHolidayLotteryNumRewardResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
